package Autodesk;

import java.io.IOException;
import java.util.Hashtable;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.MultipleOutputs;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

public class TotalVolumeFilter extends Configured implements Tool  {
	
		public Path prodPath;
		public static class LogMapper extends Mapper<Object, Text, Text, IntWritable> {
			
			private final static IntWritable one = new IntWritable(1);
			private Text word = new Text();
			Hashtable<String, String> ht = new Hashtable<String, String>();

		public void map(Object key, Text value, Context context)
				throws IOException, InterruptedException {
			StringBuffer bf = new StringBuffer();

			String record[] = value.toString().toLowerCase().split("\t");
			try{
			bf.append(record[0]);// Date.
			bf.append("\t");
			bf.append(record[1]);// Channel
			bf.append("\t");
			bf.append(record[4]);// Language
			bf.append("\t");
			bf.append(record[3]);// Product
			bf.append("\t");
			bf.append(record[8]);// Country

			word.set(bf.toString());
			context.write(word, one);
			}catch(Exception e){
				System.out.println("Erroor Reg: "+value);
			}
		}
	}

	public static class LogReducer extends
			Reducer<Text, IntWritable, Text, IntWritable> {
		
		private IntWritable result = new IntWritable();
		private MultipleOutputs<Text,IntWritable> mos;
		 
		 public void setup(Context context) {
			 mos = new MultipleOutputs<Text,IntWritable>(context);
		 }
		public void reduce(Text key, Iterable<IntWritable> values,
				Context context) throws IOException, InterruptedException {
			
			int sum = 0;
			for (IntWritable val : values) {
				sum += val.get();
			}
			result.set(sum);
			String[] keySplitted=key.toString().split("\t",2);
			mos.write(key, result, keySplitted[0]+"_VolumeReport");
			//context.write(key, result);
		
		}
		 public void cleanup(Context context) throws IOException, InterruptedException {
			 mos.close();
		 }
	}
	
	public int run(String[] args) throws Exception { 
		Configuration conf = getConf();
		
		Job job = new Job(conf, "VolumeFilter");
		job.setJarByClass(Autodesk.TotalVolumeFilter.class);

		job.setMapperClass(LogMapper.class);
		job.setReducerClass(LogReducer.class);
		
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(IntWritable.class);

	    FileInputFormat.setInputPaths(job, new Path(args[0]));
	    FileOutputFormat.setOutputPath(job, new Path(args[1]) );
	    
	    String inputPathName=args[0].toString();
	    inputPathName=inputPathName.substring(inputPathName.indexOf("_S.")+3);
	    System.out.println(inputPathName);
	    job.getConfiguration().set("pathName", inputPathName);
	    
	    MultipleOutputs.addNamedOutput(job,"VolumeFilter", TextOutputFormat.class, Text.class, IntWritable.class);

		System.exit(job.waitForCompletion(true)?0:1);
		
		return 0;
	
	}
	
	public static void main(String[] args) throws Exception {
		int res = ToolRunner.run(new Configuration(), new TotalVolumeFilter(), args);
		System.exit(res);
		}

 
}